package com.xjrsoft.module.customerTwo.EMP.settlement.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.Enum.GenderEnum;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.customerTwo.EMP.checkin_log.entity.TElectricCheckinLog;
import com.xjrsoft.module.customerTwo.EMP.checkin_log.vo.TElectricCheckinLogListVo;
import com.xjrsoft.module.customerTwo.EMP.settlement.dto.*;
import com.xjrsoft.module.customerTwo.EMP.settlement.entity.TElectricCheckoutSettlementTools;
import com.xjrsoft.module.customerTwo.EMP.settlement.vo.*;
import com.xjrsoft.module.customerTwo.EMP.settlement.entity.TElectricCheckoutSettlementDetails;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.EMP.settlement.entity.TElectricCheckoutSettlement;
import com.xjrsoft.module.customerTwo.EMP.settlement.service.ITElectricCheckoutSettlementService;

/**
 *  控制器
 *
 * @author lisen
 * @since 2023-08-04
 */
@RestController
@AllArgsConstructor
@RequestMapping("/settlement")
@Api(value = "退宿结算", tags = "退宿结算接口")
public class TElectricCheckoutSettlementController {


	private final ITElectricCheckoutSettlementService tElectricCheckoutSettlementService;


	/**
	 * 详情 
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入tElectricCheckoutSettlement")
	@SaCheckPermission(value = {"settlement:view", "settlement:modify"}, mode = SaMode.OR)
	public Response<TElectricCheckoutSettlementFormDataVo> getFormData(@PathVariable String id) {
		TElectricCheckoutSettlementFormDataVo formDataVo = new TElectricCheckoutSettlementFormDataVo();
		// 主表数据
		TElectricCheckoutSettlement tElectricCheckoutSettlement = tElectricCheckoutSettlementService.getById(id);
		TElectricCheckoutSettlementVo tElectricCheckoutSettlementVo = BeanUtil.copy(tElectricCheckoutSettlement, TElectricCheckoutSettlementVo.class);
		tElectricCheckoutSettlementVo.setGender("未定义");
		if(tElectricCheckoutSettlement.getGender()!=null){
			if(tElectricCheckoutSettlement.getGender().equals(1)){
				tElectricCheckoutSettlementVo.setGender("女");
			}else{
				tElectricCheckoutSettlementVo.setGender("男");
			}
		}

		formDataVo.setTElectricCheckoutSettlementVo(tElectricCheckoutSettlementVo);
		// 从表数据
		List<TElectricCheckoutSettlementDetailsVo> tElectricCheckoutSettlementDetailsVoList = BeanUtil.copyList(tElectricCheckoutSettlementService.getTElectricCheckoutSettlementDetailsByParentId(id), TElectricCheckoutSettlementDetailsVo.class);
		formDataVo.setTElectricCheckoutSettlementDetailsVoList(tElectricCheckoutSettlementDetailsVoList);

		List<TElectricCheckoutSettlementToolsVo> tElectricCheckoutSettlementToolsVoList = BeanUtil.copyList(tElectricCheckoutSettlementService.getTElectricCheckoutSettlementToolsByParentId(id), TElectricCheckoutSettlementToolsVo.class);
		formDataVo.setTElectricCheckoutSettlementToolsVoList(tElectricCheckoutSettlementToolsVoList);
		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入tElectricCheckoutSettlement")
		public Response<PageOutput<TElectricCheckoutSettlementListVo>> getPageList(TElectricCheckoutSettlementListDto listDto) {
		IPage<TElectricCheckoutSettlement> page = tElectricCheckoutSettlementService.getPageList(listDto);
//		List<TElectricCheckoutSettlementListVo> records = BeanUtil.copyList(page.getRecords(), TElectricCheckoutSettlementListVo.class);
//		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
		List<TElectricCheckoutSettlement> records = BeanUtil.copyList(page.getRecords(), TElectricCheckoutSettlement.class);
		// 转换列表数据
		List<TElectricCheckoutSettlementListVo> voList = BeanUtil.copyListProperties(records, TElectricCheckoutSettlementListVo::new, (tElectricCheckoutSettlement, tElectricCheckoutSettlementListVo) ->{
			// 这里可以定义特定的转换规则
			tElectricCheckoutSettlementListVo.setGender(GenderEnum.getDescByCode(tElectricCheckoutSettlement.getGender()==null?0:tElectricCheckoutSettlement.getGender()).getDesc());
		});
		DataTransUtil.transListShowData(voList);
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), voList));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入tElectricCheckoutSettlement")
	@SaCheckPermission("settlement:add")
	public Response save(@RequestBody SaveTElectricCheckoutSettlementFormDataDto formDto) throws Exception {
		TElectricCheckoutSettlementDto tElectricCheckoutSettlementDto = formDto.getTElectricCheckoutSettlementDto();
		TElectricCheckoutSettlement tElectricCheckoutSettlement = BeanUtil.copy(tElectricCheckoutSettlementDto, TElectricCheckoutSettlement.class);
		List<TElectricCheckoutSettlementDetailsDto> tElectricCheckoutSettlementDetailsDtoList = formDto.getTElectricCheckoutSettlementDetailsDto();
		List<TElectricCheckoutSettlementDetails> tElectricCheckoutSettlementDetailsList = BeanUtil.copyList(tElectricCheckoutSettlementDetailsDtoList, TElectricCheckoutSettlementDetails.class);

		List<TElectricCheckoutSettlementToolsDto> tElectricCheckoutSettlementToolsDtoList = formDto.getTElectricCheckoutSettlementToolsDto();
		List<TElectricCheckoutSettlementTools> tElectricCheckoutSettlementToolsList = BeanUtil.copyList(tElectricCheckoutSettlementToolsDtoList, TElectricCheckoutSettlementTools.class);
		boolean isSuccess = tElectricCheckoutSettlementService.addTElectricCheckoutSettlement(tElectricCheckoutSettlement, tElectricCheckoutSettlementDetailsList,tElectricCheckoutSettlementToolsList);
		return Response.status(isSuccess);
	}

	@PostMapping("/settle/{id}")
	@ApiOperation(value = "结算", notes = "传入id")
	@SaCheckPermission("settlement:settle")
	public Response save(@PathVariable String id) {
		return Response.status(tElectricCheckoutSettlementService.settle(id));
	}

	@PostMapping("/deprecated/{id}")
	@ApiOperation(value = "结算", notes = "传入id")
	@SaCheckPermission("settlement:settle")
	public Response deprecated(@PathVariable String id) throws Exception {
		return Response.status(tElectricCheckoutSettlementService.deprecated(id));
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入tElectricCheckoutSettlement")
	@SaCheckPermission("settlement:modify")
	public Response update(@PathVariable String id, @RequestBody SaveTElectricCheckoutSettlementFormDataDto formDto) {
		TElectricCheckoutSettlementDto tElectricCheckoutSettlementDto = formDto.getTElectricCheckoutSettlementDto();
		TElectricCheckoutSettlement tElectricCheckoutSettlement = BeanUtil.copy(tElectricCheckoutSettlementDto, TElectricCheckoutSettlement.class);

		List<TElectricCheckoutSettlementDetailsDto> tElectricCheckoutSettlementDetailsDtoList = formDto.getTElectricCheckoutSettlementDetailsDto();
		List<TElectricCheckoutSettlementDetails> tElectricCheckoutSettlementDetailsList = BeanUtil.copyList(tElectricCheckoutSettlementDetailsDtoList, TElectricCheckoutSettlementDetails.class);


		List<TElectricCheckoutSettlementToolsDto> tElectricCheckoutSettlementToolsDtoList = formDto.getTElectricCheckoutSettlementToolsDto();
		List<TElectricCheckoutSettlementTools> tElectricCheckoutSettlementToolsList = BeanUtil.copyList(tElectricCheckoutSettlementToolsDtoList, TElectricCheckoutSettlementTools.class);
		return Response.status(tElectricCheckoutSettlementService.updateTElectricCheckoutSettlement(id, tElectricCheckoutSettlement, tElectricCheckoutSettlementDetailsList,tElectricCheckoutSettlementToolsList));
	}



	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	@SaCheckPermission(value = {"settlement:batchDelete", "settlement:delete"}, mode = SaMode.OR)
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(tElectricCheckoutSettlementService.delete(ids));
	}

	@PostMapping("/confirm/{id}")
	@ApiOperation(value = "确认", notes = "传入id")
	@SaCheckPermission("settlement:lock")
	public Response confirm(@PathVariable String id) {
		return Response.status(tElectricCheckoutSettlementService.confirm(id));
	}
}
